import unittest, requests
# 导入数据库类
from Mysql import mysql
# 导入装饰器
from ddt import ddt,file_data

@ddt()
class Weidu(unittest.TestCase):
    def setUp(self) -> None:
        print('start case')
        # 初始化数据库类
        self.db = mysql('10.59.9.19', 'yidongceshi', 'Yd123456!', 'mall')

    def tearDown(self) -> None:
        print('end case')
        self.db.close()

    # 登录
    userId = None
    sessionId = None
    def test_Weidu_01(self):
        r1 = requests.post(url='http://10.59.9.18/small/user/v1/login',
                           json={"phone": "15535738398", "pwd": "zjj031613"})
        print(r1.json())
        # 提取userId sessionId
        Weidu.userId = r1.json()['result']['userId']
        Weidu.sessionId = r1.json()['result']['sessionId']
        # 断言 提取phone
        Weidu.phone = r1.json()['result']['phone']
        print(Weidu.phone)
        result = self.db.chaxun(f'select * from user where phone={str(Weidu.phone)}')
        print(result)
        # 提取用户名 进行对比和数据库
        nickName = r1.json()['result']['nickName']
        print(nickName)
        self.assertEqual(nickName, result[0][1])

    # 添加新收货地址
    addres_id = None
    @file_data('data.json')
    def test_Weidu_02(self,realName,phone,address,zipCode,casename):
        if casename == '正':
            r2 = requests.post(url='http://10.59.9.18/small/user/verify/v1/addReceiveAddress',
                               headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)},
                               data={"realName": realName, "phone": phone,
                                     "address": address, "zipCode": zipCode})
            # 断言 查看数据库是否存在新添加的收获地址
            zipCode = 101077
            result = self.db.chaxun(f'select * from receive_address where zip_Code={zipCode}')
            print(result)
            self.assertIn("北京昌平区南口镇21号", result[0][4])
            message = r2.json()['message']
            self.assertIn('添加成功', message)
        elif casename == '反':
            r2= requests.post(url='http://10.59.9.18/small/user/verify/v1/addReceiveAddress',
                               headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)},
                               data={"realName": realName, "phone": phone,
                                     "address": address, "zipCode": zipCode})
        print(r2.json())

if __name__ == '__main__':
    unittest.main()